var baseUrl = require('../../utils/urllink.js')

var app = getApp()
Page({
  data: {
    showTopTips: false,
    showTopTipsText:'',

    countryCodeIndex: 0,
    countryIndex: 0,
    accountIndex: 0,
    isAgree: false,
    files: [],
    files2: [],

    images1:'',
    images2: '',
    
    shanghu:'',
    xiangmu:'',
    jieshao:'',
    lianxiren: '',
    phone: '',
    wx_code:'',
    panduan:0,
    hostimgs: app.globalData.hostimgs,
  },

  onLoad() {
    var that = this
    var store_id = app.globalData.store_id;
    if (store_id > 0){
      //已经有店铺资料了，
      wx.showModal({
        title: '提示',
        content: '已经填写过开店资料，可直接申请',
        showCancel:false,
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
            //直接通过申请供货商
            guosheng(that, app.globalData.user_id, app.globalData.store_id)
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }else{
      //请求服务器 开店申请
      wx.request({
        url: baseUrl + 'weapp/Users/info.html',
        data: {
          user_id: app.globalData.user_id,
        },
        method: 'GET',
        header: {
          'content-type': 'application/json'
        }, // 设置请求的 header
        success: function (res) {
          console.log(res.data)
          that.setData({
            userInfo: res.data,
            phone: res.data.mobile
          })
        },
        fail: function () {
        },
        complete: function () {
          // complete
        }
      })
    }

  },




  //表单提交
  formSubmit:function(e){
    var that = this;
    console.log(e.detail.value.shanghu);

    var shanghu = e.detail.value.shanghu;
    var xiangmu = e.detail.value.xiangmu;
    var jieshao = e.detail.value.jieshao;
    var lianxiren = e.detail.value.lianxiren;
    var phone = e.detail.value.phone;
    var wx_code = e.detail.value.wx_code;

    if (shanghu.length <=0) {
    this.setData({
      showTopTipsText: '请输入商户名称',
      showTopTips: true
    })
    setTimeout(function () {
      that.setData({
        showTopTips: false
      });
    }, 3000);
    } else if (xiangmu.length <= 0) {
    this.setData({
      showTopTipsText: '请输入主营项目',
      showTopTips: true
    })
    setTimeout(function () {
      that.setData({
        showTopTips: false
      });
    }, 3000);
    } else if (lianxiren.length <= 0) {
      this.setData({
        showTopTipsText: '请输入联系人',
        showTopTips: true
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    } else if (phone.length <= 0) {
      this.setData({
        showTopTipsText: '请输入手机号码',
        showTopTips: true
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    }else{
      //请求服务器
      wx.request({
        url: baseUrl +'weapp/Store/add_store_data.html',
        data: {
          user_id: app.globalData.user_id,
          store_cert: that.data.images1,
          store_logo: that.data.images2,
          wx_code: wx_code,
          store_name: shanghu,
          store_zy: xiangmu,
          store_desc: jieshao,
          user_name: lianxiren,
          store_phone: phone,

        },
        method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
        header: {
          'content-type': 'application/json'
        }, // 设置请求的 header
        success: function (res) {
          console.log(res.data)
          if (res.data.code == 0) {
            wx.showModal({
              title: '提示',
              content: '提交成功',
              showCancel: false,
              success: function (res) {
                if (res.confirm) {
                  console.log('用户点击确定')
                  wx.switchTab({
                    url: '../mine/mine',
                  })
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
          } else if(res.data.code == -1){
            console.log("保存错误")
            wx.showModal({
              title: '提示',
              content: '提交错误',
              showCancel: false,
              success: function (res) {
                if (res.confirm) {
                  console.log('用户点击确定')

                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
          }
        },
        fail: function () {
        },
        complete: function () {
        }
      })
    }

 



    
  },

  //上传图片 显示功能
  chooseImage: function (e) {
    var that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        that.setData({
          files: [],
        })
        that.setData({
          files: that.data.files.concat(res.tempFilePaths)
        })
        sendPhotos(res.tempFilePaths, that, app.globalData.openid, app.globalData.user_id, 1)
      }
    })
  },
  previewImage: function (e) {
    wx.previewImage({
      current: e.currentTarget.dataset.id, // 当前显示图片的http链接
      urls: this.data.files // 需要预览的图片http链接列表
    })
  },

  chooseImage2: function (e) {
    var that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        that.setData({
          files2: [],
        })
        that.setData({
          files2: that.data.files2.concat(res.tempFilePaths)
        })
        sendPhotos(res.tempFilePaths, that, app.globalData.openid, app.globalData.user_id, 2)
      }
    })
  },
  previewImage2: function (e) {
    wx.previewImage({
      current: e.currentTarget.dataset.id, // 当前显示图片的http链接
      urls: this.data.files2 // 需要预览的图片http链接列表
    })
  },

  

  

});



//上传图片
function sendPhotos(arr, that, openid, user_id, images_id) {
  wx.showToast({
    icon: "loading",
    title: "正在上传"
  })

  wx.uploadFile({
    url: baseUrl +'weapp/SavePicApi/save_shoppic.html',
    filePath: arr[0],
    name: 'file',
    formData: {
      //和服务器约定的token, 一般也可以放在header中
      'openid': openid,
      'user_id': user_id
    },
    header: { "Content-Type": "multipart/form-data" },
    success: function (res) {
      console.log(JSON.parse(res.data));
      if (images_id == 1) {
        var images1 = JSON.parse(res.data);
        that.setData({
          images1: images1,
        })
      }
      if (images_id == 2) {
        var images2 = JSON.parse(res.data);
        that.setData({
          images2: images2,
        })
      }
      wx.showToast({
        icon: "success",
        title: "上传成功"
      })

    }

  })



}

//直接申请成为供货商
function guosheng(that,user_id,store_id){
  //请求服务器 
  wx.request({
    url: baseUrl + 'weapp/Store/modi_agent_type.html',
    data: {
      user_id: user_id,
      store_id: store_id,
    },
    method: 'GET',
    header: {
      'content-type': 'application/json'
    }, // 设置请求的 header
    success: function (res) {
      console.log(res.data)
      wx.switchTab({
        url: '../mine/mine',
      })
    },
    fail: function () {
    },
    complete: function () {
      // complete
    }
  })
}